package com.kxp.easyatm.controller;

import com.kxp.easyatm.entity.Custom;
import com.kxp.easyatm.service.CustomService;
import com.kxp.easyatm.service.impl.CustomServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author longhunpomie
 * @declaration
 * @email 3314028177@qq.com
 * @create 2023/11/25-10:29
 */
@WebServlet(name = "customServlet", value = "/custom")
public class CustomServlet extends BaseServlet{

    private CustomService customService = new CustomServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        String pin = req.getParameter("pin");
        Custom login = customService.login(account, pin);

        if(login != null){
            System.out.println("登录成功");
            HttpSession session = req.getSession();
            session.setAttribute("user", login);
            req.getRequestDispatcher("/pages/custom/login_success.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg", "用户名或密码错误！");
            req.setAttribute("account", account);
            System.out.println("登录失败");
            req.getRequestDispatcher("/pages/custom/login.jsp").forward(req, resp);
        }
    }

    protected void logout (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.getSession().invalidate();//销毁Session;
        response.sendRedirect(request.getContextPath());
    }
}
